//
//  TwoWayCycleLinkLisk.c
//  algorithmDemo
//
//  Created by 李文深 on 16/8/21.
//  Copyright © 2016年 30pay. All rights reserved.
//

#include "TwoWayCycleLinkLisk.h"
#include "FatalError.h"
#include <stdlib.h>

List initAlphabet() {
    
    List head = (List)malloc(sizeof(struct Node));
    
    if (head == NULL) {
        FatalError("初始化失败");
        return NULL;
    }
    
    Node *preEle = head;
    
    for (int i = 65; i<= 90; i++) {
        
        Node *aNode = (Node *)malloc(sizeof(struct Node));
        aNode->data = i;
        aNode->pre = preEle;
        aNode->next = NULL;
        preEle->next = aNode;
        preEle = aNode;
    }
    
    //把最后一个元素连接到第一个元素
    Node *first = head->next;
    Node *last = preEle;
    last->next = first;
    first->pre = last;
    
    //释放头结点
    free(head);
    
    return first;
}

void printAlphabet(List l,int index) {
    
    Node *p = l;
    
    for (int i = 0; i < (index % 26); i++) {
        p = p->next;
    }
    
    Node *first = p;
    
    do {
        printf("%c\t",p->data);
        p = p->next;
        
    }  while (p != first);
}